import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

public class leetCodeResult0927 {

    @Test
    public void test() {
        //构建一个函数将数组元素以此排列建成一个链表,返回这个链表的头结点
        int[] a = {8, 0, 7, 1, 7, 7, 9, 7, 5, 2, 9, 1, 7, 3, 7, 0, 6, 5, 1, 7, 7, 9, 3, 8, 1, 5, 7, 7, 8, 4, 0, 9, 3, 7, 3, 4, 5, 7, 4, 8, 8, 5, 8, 9, 8, 5, 8, 8, 4, 7, 5, 4, 3, 7, 3, 9, 0, 4, 8, 7, 7, 5, 1, 8, 3, 9, 7, 7, 1, 5, 6, 0, 7, 3, 7, 1, 9, 2, 5, 7, 9, 7, 7, 1, 7, 0, 8};
        ListNode head = buildListNode(a);
        System.out.println(isPalindrome(head));
    }

    /**
     * 构建一个函数将数组元素以此排列建成一个链表,返回这个链表的头结点
     * @param a
     * @return
     */
    private ListNode buildListNode(int[] a) {
        ListNode head = new ListNode(a[0]);
        ListNode copyHead = head;
        for (int i = 1; i < a.length; i++) {
            ListNode node = new ListNode(a[i]);
            copyHead.next = node;
            copyHead = copyHead.next;
        }
        return head;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    /**
     * 206. 反转链表
     * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
     */
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode result = getLastNode(head);
        ListNode firstNode = buildReverseList(head);
        firstNode.next = null;

        return result;
    }

    /**
     * 递归函数寻找链表最后一个节点
     *
     * @param head
     * @return
     */
    private ListNode getLastNode(ListNode head) {
        if (head.next == null) {
            return head;
        }
        return getLastNode(head.next);
    }

    /**
     * 递归函数寻找链表最后一个节点
     */
    private ListNode buildReverseList(ListNode head) {
        ListNode nextNode;
        if (head != null && head.next != null) {
            nextNode = buildReverseList(head.next);
            nextNode.next = head;
        }
        return head;
    }

    /**
     * 给你一个单链表的头节点 head ，请你判断该链表是否为回文链表。如果是，返回 true ；否则，返回 false 。
     * 时间复杂度：O(2n) 空间复杂度：O(n)
     */
    public boolean isPalindrome(ListNode head) {
        boolean result = false;
        int listLength = getNodeListLength(head);
        LinkedList<Integer> nodeValueList = new LinkedList<>();
        while (head != null) {
            if (listLength == 1) {
                return true;
            }
            //偶数回文串
            if(listLength % 2 == 0 && !nodeValueList.isEmpty() && nodeValueList.get(nodeValueList.size() - 1) == head.val){
                boolean palindromeV = isPalindrome1(nodeValueList, head, listLength);
                if(!palindromeV && nodeValueList.size() > listLength / 2){
                    return palindromeV;
                }
                if(palindromeV){
                    return true;
                }
            }
            //奇数回文串
            if (listLength % 2 == 1 && (nodeValueList.size() >= 2 &&
                    nodeValueList.get(nodeValueList.size() - 2) == head.val)) {
                boolean palindromeV = isPalindrome2(nodeValueList, head, listLength);
                if (!palindromeV && nodeValueList.size() > listLength / 2 + 1) {
                    return palindromeV;
                }
                if(palindromeV){
                    return true;
                }
            }
            nodeValueList.add(head.val);
            head = head.next;
        }
        return result;
    }

    /**
     * 递归函数判断偶数节点数链表是否为回文链表
     * @param nodeValueList
     * @param head
     * @param listLength
     * @return
     */
    private boolean isPalindrome2(LinkedList<Integer> nodeValueList, ListNode head, int listLength) {
        if (listLength - nodeValueList.size() != nodeValueList.size() - 1) {
            return false;
        }
        //回文链表前半部分未匹配上的节点数量
        int notMatchNodeCount = nodeValueList.size() - 1;
        return isPalindrome3(head, nodeValueList, notMatchNodeCount);
    }

    private boolean isPalindrome3(ListNode head, LinkedList<Integer> nodeValueList, int notMatchNodeCount) {
        while (head != null) {
            if (head.val != nodeValueList.get(notMatchNodeCount - 1)) {
                return false;
            }
            head = head.next;
            notMatchNodeCount--;
        }
        return notMatchNodeCount == 0;
    }

    /**
     * 递归函数判断奇数节点数链表是否为回文链表
     *
     * @param nodeValueList 回文前半部分链表节点值列表
     * @param head 回文后半部分链表头节点
     * @param listLength 链表长度
     * @return
     */
    private boolean isPalindrome1(LinkedList<Integer> nodeValueList, ListNode head, int listLength) {
        if (listLength - nodeValueList.size() != nodeValueList.size()) {
            return false;
        }
        //回文链表前半部分未匹配上的节点数量
        int notMatchNodeCount = nodeValueList.size();
        return isPalindrome3(head, nodeValueList, notMatchNodeCount);
    }

    private int getNodeListLength(ListNode head) {
        if (head.next == null) {
            return 1;
        }
        return getNodeListLength(head.next) + 1;
    }
}
